<?php
include("include/config.inc.php");
$error = 0;
if(isset($_POST['userName']) && strlen($_POST['userName']) > 0 || isset($_POST['password']) && strlen($_POST['password']) > 0)
{
  $checkLogin = "SELECT userId,userName,password,userType
                   FROM user
                  WHERE userName = '".$_POST['userName']."'
                    AND password = '".$_POST['password']."'";
  $checkLoginresult = mysql_query($checkLogin);
  if($userLoginRow = mysql_fetch_array($checkLoginresult))
  {
  	$_SESSION['s_activeUserId'.$cfgPack] = $userLoginRow['userId'];
    $_SESSION['s_activId'.$cfgPack]      = $userLoginRow['userName'];
    $_SESSION['s_userType'.$cfgPack]     = $userLoginRow['userType'];
    
    $selCurDate = "SELECT date FROM relievetechnician WHERE date = '".date('Y-m-d')."' ";
    $selCurDateRes = mysql_query($selCurDate);
    if(mysql_num_rows($selCurDateRes) == 0)
    {
    	$selTechAllocated = "SELECT complainServiceId,newinstallationMasterId	,otherServiceId
    	                       FROM techallocation
    	                      WHERE date = '".date('Y-m-d')."' ";
      $selTechAllocatedRes = mysql_query($selTechAllocated);
      while($techRow = mysql_fetch_array($selTechAllocatedRes))
      {
      	if($techRow['complainServiceId'] > 0)
      	{
      		$updateTable = "complainservice";
      		$updateCondition = " complainServiceId = ".$techRow['complainServiceId']."";
      	}
      	else if($techRow['newinstallationMasterId'] > 0)
      	{
      		$updateTable = "newinstallationmaster";
      		$updateCondition = " newinstallationMasterId = ".$techRow['newinstallationMasterId']."";
      	}
      	else if($techRow['otherServiceId'] > 0)
      	{
      		$updateTable = "otherservice";
      		$updateCondition = " otherServiceId = ".$techRow['otherServiceId']."";
      	}
      	
      	$updateTech = "UPDATE ".$updateTable."
      	                  SET techAllocated = 'N'
      	                WHERE ".$updateCondition." ";
      	$updateTechRes = mysql_query($updateTech);
      	
      }
    	                       
	    $delQuery = "DELETE FROM  techallocation
	                  WHERE date = '".date('Y-m-d')."' ";
	    $delQueryRes = mysql_query($delQuery);
	   	
    	$insertDate = "INSERT INTO relievetechnician (date, userId)
    	               VALUES ('".date('Y-m-d')."','".$userLoginRow['userId']."')";
    	$insertDateRes = mysql_query($insertDate);
    }
    
    if(!$_SESSION['s_urlRedirectDir'])
      header("Location:./index.php");
    else
      header("Location:".$_SESSION['s_urlRedirectDir']);
  }
  else
  {
    $error = 1;
  }
}

include("./bottom.php");
$smarty->assign("error",$error);
$smarty->display("checkLogin.tpl");
?>